package com.jxwifi.cloud.quickclean.popwindow;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.CountDownTimer;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Base64;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.idroid.utils.NoDoubleClickListener;
import com.jxwifi.cloud.quickclean.R;
import com.jxwifi.cloud.quickclean.adapter.JoinAdapter;
import com.jxwifi.cloud.quickclean.captcha.Captcha;
import com.jxwifi.cloud.quickclean.utils.TokenTimeUtils;
import com.popwindow.BasicPopmodule;

import java.util.List;

import butterknife.internal.DebouncingOnClickListener;

/**
 * 滑动拼图弹框
 */
public class PopCaptcha extends BasicPopmodule {

    private String TAG = this.getClass().getSimpleName();
    private Context mContext;
    private PopCaptchatListener mPoplistener;
    private String captchaCode;
    private String cutImage;
    private String srcImage;
    private int xposition;
    private int yposition;
    private ImageView close_captcha;

    public PopCaptcha(Context mContext,String captchaCode, String cutImage, String srcImage, int xposition, int yposition,  PopCaptchatListener mPoplistener) {
        this.mContext = mContext;
        this.mPoplistener = mPoplistener;
        this.captchaCode = captchaCode;
        this.cutImage = cutImage;
        this.srcImage = srcImage;
        this.xposition = xposition;
        this.yposition = yposition;
        initView();
    }

    @Override
    public void setPopwindow(PopupWindow pop) {
        mPopwindows = pop;
    }

    @Override
    public View getView() {
        return mContentView;
    }

    private void initView() {
        mContentView = LayoutInflater.from(mContext).inflate(R.layout.pop_captcha, new LinearLayout(mContext), false);
        setListener();
    }

    public void setListener(){
        View mVwPopBlan = (View) mContentView.findViewById(R.id.vw_pop_blank);
        close_captcha = (ImageView) mContentView.findViewById(R.id.close_captcha);
        Captcha captCha = (Captcha) mContentView.findViewById(R.id.captCha);
        captCha.setBitmap(stringToBitmap(srcImage));
        //captCha.setBitmap(R.mipmap.cat);
        captCha.setVertifyBlockitmap(stringToBitmap(cutImage));
        captCha.setBlockInfo(xposition,yposition*3-9);
        //captCha.setBlockInfo(Math.round(xposition*density),Math.round(yposition*density));
        captCha.setCaptchaListener(new Captcha.CaptchaListener() {
            @Override
            public String onAccess(long time) {
                Toast.makeText(mContext, "验证成功", Toast.LENGTH_SHORT).show();
                return "验证通过";
            }

            @Override
            public String onFailed(int count) {
                Toast.makeText(mContext, "验证失败,失败次数" + count, Toast.LENGTH_SHORT).show();
                return "验证失败";
            }

            @Override
            public String onMaxFailed() {
                Toast.makeText(mContext, "验证超过次数，你的帐号被封锁", Toast.LENGTH_SHORT).show();
                return "可以走了";
            }

            @Override
            public void getXp(int x) {

            }

            @Override
            public void refresh() {

            }

        });

        close_captcha.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopwindows.dismiss();
                mPoplistener.dissPop();
            }
        });

        mVwPopBlan.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                //mPopwindows.dismiss();
            }
        });

    }


    public interface PopCaptchatListener {
        void dissPop();
    }
    /**
     * 解码Base64图片
     * @param string
     * @return
     */
    public Bitmap stringToBitmap(String string) {
        Bitmap bitmap = null;
        try {
            //byte[] bitmapArray = Base64.decode(string.split(",")[1], Base64.DEFAULT);
            byte[] bitmapArray = Base64.decode(string, Base64.DEFAULT);
            bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }


}
